
<script>
	import {
		APP_NAME,
		HTTP_REQUEST_URL
	} from 'config.js'
	import {
		getSystemInfo
	} from '@/common/promise.js' 
	import {
		viewHotFixInit 
	} from '@/common/updateModel.js'
	import WebSocket from '@/common/websocket.js'
	export default {
		globalData: {   
			isIphoneX: false,
			app_name: APP_NAME,
			userInfo: {},
			version: '',
			old_version: '',
			versionCode: 0,
			old_versionCode: 0,
			systemInfo: {},
			version_update_data: {
				version: '',
				url: ''
			},
			web_host: HTTP_REQUEST_URL, 
			res_data:{},
			isNoticeOpen:false,
			systemInfo:{}
		},
		onLaunch:async function() { 		
			
			this.getSystemInfo();   // 获取手机信息
			
			
			// #ifdef APP-PLUS    
			let timer = false;	
					
			// plus.push.addEventListener("receive", (msg) => { 
			// 	if (msg.type == 'receive') {
			// 		var options = {
			// 			cover: false,
			// 			title: msg.title
			// 		};
			// 		plus.push.createMessage(msg.content, msg.payload, options);
			// 	} 
			// }, false)	

			// plus.push.addEventListener("click", (msg) => { 	 
			// 	clearTimeout(timer);
			// 	timer = setTimeout(() => {
			// 		if (msg.payload) {
			// 			uni.navigateTo({
			// 				url: msg.payload
			// 			})					
			// 		}
			// 	}, 500) 
			// }, false)

					

			// #endif
		},
		onShow: async function() { 	 
			
			if(uni.getStorageSync('token')){
				WebSocket.checkSocketState();
			}
			
			
			// 菜单未读消息角标
			// uni.setTabBarBadge({
			// 	index: 1,
			// 	text:JSON.stringify(this.$store.state.new_msg_num) 
			// })
			
			
			this.checkPhoneType();
			// this.getUserAuth();	
			  	 
			// this.getOpenId();

			// #ifdef APP-PLUS
			// this.getCid();
			
			this.checkVersion();    // 检测版本
			
			
			// 进入应用后清除角标
			// if(plus.os.name=='Android'){
			// 	plus.runtime.setBadgeNumber(-1); 
			// }else{
			// 	plus.runtime.setBadgeNumber(0)
			// }		
			
			// this.checkNoticeOpen();   // 是否开启通知检测 
			// #endif
		},
		onHide: function() {
			

		},
		methods: { 
		

			// 获取用户cid  消息推送使用
			getCid: function() {
				let _this = this;
				setTimeout(async () => { // 定时器的作用是防止获取cid有延迟 
					
					let pinf = plus.push.getClientInfo();
					let cid = pinf && pinf.clientid || ''; //客户端标识 
 
					plus.push.getClientInfoAsync((info) => {
						cid = info.clientid;  
					}, err => {
					}); 
					
					let res = await _this.$api.user_api.sendCid({
						cid: cid,
						user_id: _this.globalData.userInfo.id,
						phone: _this.globalData.userInfo.phone,
						alltype: _this.globalData.userInfo.alltype,
						province: _this.globalData.userInfo.province,
						city: _this.globalData.userInfo.city,
						district: _this.globalData.userInfo.district,
						town: _this.globalData.userInfo.town,
						province_ysb: _this.globalData.userInfo.province_ysb,
						city_ysb: _this.globalData.userInfo.city_ysb,
						district_ysb: _this.globalData.userInfo.district_ysb,
						town_ysb: _this.globalData.userInfo.town_ysb,
						ysb_type: _this.globalData.userInfo.ysb_type,
					})  
					this.globalData.res_data=res; 
					
				}, 1000);
			},
			
			// 获取消息
			async getMessageList(){ 
				let res=await this.$api.user_api.addMsgTypeList();  
				if(res.data.code==200){  
					res.data.data.forEach(item=>{
						this.new_msg_num+=Number(item.count)
					})				 	
				}
			}, 

			// 获取用户权限信息
			// getUserAuth: async function() {
			// 	let res = await this.$api.user_api.getUserInfo();    
			// 	if (res.data.ret == 0) {    
			// 		this.globalData.userInfo = res.data.data; 
			// 		this.checkSspRedPack(res.data.data.wx_uuid);
			// 		this.checkRedPacket(res.data.data.wx_open_id);	
			// 		// if(this.globalData.userInfo.is_nx==1){
			// 		// 	this.checkRedPacket(res.data.data.wx_open_id);						 
			// 		// }						
			// 	}
			// },

			// 获取终端系统信息
			getSystemInfo: async function() {
				let res = await getSystemInfo();
				this.globalData.systemInfo=res; 
				uni.setStorage({
					key:"systemInfo",
					data:res
				})
			},

			// 检测手机型号
			checkPhoneType: async function() {
				let res = await getSystemInfo();
				if (res.model.search('iPhone X') != -1 || res.model.search('iPhone 11') != -1) {
				 this.globalData.isIphoneX = true;
				} else {
					this.globalData.isIphoneX = false;
				}
			},

			// 检测版本
			checkVersion: async function() {
				let _this = this;
				 
				let res = await this.$api.user_api.getVersion();  
				console.log(res)
				if (res.data.code == 200) {				
					
					this.globalData.version = res.data.data.version;
					this.globalData.versionCode = res.data.data.versionCode;
 					
					if (!uni.getStorageSync('now_version')) { // 判断是否已有版本更新记录
					
						// 默认缓存第一个版本
						uni.setStorage({
							key: 'now_version',
							data: this.globalData.version
						})
						uni.setStorage({
							key: 'now_versionCode',
							data: this.globalData.versionCode
						});
						return
					}
					
					
					
					_this.globalData.old_version = uni.getStorageSync('now_version');
					_this.globalData.old_versionCode = uni.getStorageSync('now_versionCode');
					
				 
					let reg2 = /[.]/ig; // 匹配全部 '.' =>''
					let new_version = this.globalData.version.replace(reg2, ""); //  1.0 =>10  版本格式转数字类型
					let old_version = this.globalData.old_version.replace(reg2, ""); //  1.0 =>10  版本格式转数字类型	 
					let new_version_arr = this.globalData.version.split('.'); // 1.0.0 => [1,0,0]  版本转换成数组
					let old_version_arr = this.globalData.old_version.split('.'); // 1.0.0 => [1,0,0]  版本转换成数组
					let log_data = res.data.data.remark.split(';'); // 更新日志
					
					
					if(plus.os.name=="Android") {	
						 
						if (parseInt(new_version) > parseInt(old_version) || this.globalData.versionCode > _this.globalData
							.old_versionCode || new_version_arr[0] > old_version_arr[0] || new_version_arr[1] >
							old_version_arr[1]) { 	 
							// 安卓自动更新
							let system = await getSystemInfo(); //  获取系统信息
							viewHotFixInit({
								systemInfo: system,
								log_data: log_data,
								version: _this.globalData.version,
								versionCode: _this.globalData.versionCode,
								apk_path: res.data.data.file_path
							})
							
							return 
							
							if (new_version_arr[0] > old_version_arr[0] || new_version_arr[1] > old_version_arr[1]) { 
								// 大版本版本更新   弹窗更新提示
								
								let system = await getSystemInfo(); //  获取系统信息
								viewHotFixInit({
									systemInfo: system,
									log_data: log_data,
									version: _this.globalData.version,
									versionCode: _this.globalData.versionCode,
									apk_path: res.data.data.file_path
								})
								return
							}							

							uni.showLoading({
								title: '应用更新中...',
								mask: true
							})
							
							uni.downloadFile({
								url: res.data.data.newfile,
								success: async (res_down) => {
									uni.hideLoading();
									if (res_down.statusCode === 200) {
										uni.hideLoading();
										plus.nativeUI.showWaiting("正在更新文件...");
							
										plus.runtime.install(res_down.tempFilePath, {
											force: true
										}, async function() {
												plus.nativeUI.closeWaiting();
												uni.setStorage({
													key: 'now_version',
													data: _this.globalData.version
												});
												uni.setStorage({
													key: 'now_versionCode',
													data: _this.globalData.versionCode
												});
							
												setTimeout(() => {
													plus.runtime.restart();
												}, 1000)
											},
											function(e) { 
												uni.showToast({
													icon: 'error',
													image: '../static/images/error.png',
													title: `热更新失败:${e.message}`,
													mask: true,
													duration: 2000
												})
											});
									}
								}
							});	
										
							
						
						} 
						
						
					}
					
					if(plus.os.name=="iOS"){					
						
						if (res.data.ios_audit==1 && (parseInt(new_version) > parseInt(old_version) || this.globalData.versionCode > _this.globalData
							.old_versionCode || new_version_arr[0] > old_version_arr[0] || new_version_arr[1] >	old_version_arr[1])) { 
								
							uni.setStorage({
								key: 'now_version',
								data: this.globalData.version
							})
							uni.setStorage({
								key: 'now_versionCode',
								data: this.globalData.versionCode
							});
						} 
						
					}
					
					
				}

			},
			
			// 检测是否开启通知
			// checkNoticeOpen:async function(){ 
				 
			// 	// 判断客户是否开启通知，如果没开启，则跳转到设置页面，start
			// 	if(plus.os.name === "Android"){
			// 		var main = plus.android.runtimeMainActivity();
			// 		var pkName = main.getPackageName();  				
			// 		var NotificationManagerCompat = plus.android.importClass("android.support.v4.app.NotificationManagerCompat");  				
			// 		if (NotificationManagerCompat == null) { 
			// 			NotificationManagerCompat = plus.android.importClass("androidx.core.app.NotificationManagerCompat");  //android.support.v4升级为androidx
			// 		}  
			// 		var packageNames = NotificationManagerCompat.from(main);   
			// 		if (packageNames.areNotificationsEnabled()) {   
			// 			this.globalData.isNoticeOpen=true; 
			// 		}else{    
			// 			this.globalData.isNoticeOpen=false; 
			// 		}  			
			// 	}else if(plus.os.name === "iOS"){
			// 		var UIApplication = plus.ios.import("UIApplication"); 
			// 		var app = UIApplication.sharedApplication();  
			// 		var enabledTypes = 0;  
			// 		if (app.currentUserNotificationSettings) {   
			// 			var settings = app.currentUserNotificationSettings();  
			// 			enabledTypes = settings.plusGetAttribute("types");   
			// 			if (enabledTypes == 0) { 
			// 				this.globalData.isNoticeOpen=false; 							 
			// 			}else{
			// 				this.globalData.isNoticeOpen=true; 
			// 			} 
			// 		} else {   
			// 			enabledTypes = app.enabledRemoteNotificationTypes();  
			// 			if(enabledTypes == 0){  
			// 				this.globalData.isNoticeOpen=true; 
			// 				console.log("推送未开启!");  
			// 			}else{  
			// 				this.globalData.isNoticeOpen=false; 
			// 				console.log("已经开启推送功能!")  
			// 			}   
			// 		}   
					
			// 		return 
					   
			// 	}
				
			// },
			
			 
			
		}


	}
</script>

 
<style>   
	
	@import url("/static/style/css/base.css");
	@import url("/static/style/css/form.css");
	@import url("/static/style/css/iconfont.css");
	@import url("/static/style/css/module.css");
	@import url("/static/style/css/popup.css");
	@import url("/static/style/css/animate.min.css"); 
</style>

<style lang="scss"> 	 
	@import "@/node_modules/uview-ui/index.scss";		 
</style>


